﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestVideoGDI
{
    public partial class Form1 : Form
    {
        const int BmpCount = 5;
        Timer T = new Timer();

        Bitmap[] Bmps = new Bitmap[25];
        Random R = new Random();
        Size SizeBmp = new Size(1920, 1080);
        public Form1()
        {

            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);

            InitializeComponent();

            T.Tick += new EventHandler(T_Tick);

            T.Interval = 32;

            for (int i = 0; i < BmpCount; i++)
            {
                Bmps[i] = new Bitmap(SizeBmp.Width, SizeBmp.Height);

                for (int y = 0; y < SizeBmp.Height; y++)
                {
                    for (int x = 0; x < SizeBmp.Width; x++)
                    {
                        Bmps[i].SetPixel(x, y, Color.FromArgb(R.Next(256), R.Next(256), R.Next(256)));
                    }
                }
            }


        }
        DateTime DT;
        int ticks = 0;
        void T_Tick(object sender, EventArgs e)
        {
            this.Invalidate();

            ticks++;
            long span = DateTime.Now.Ticks - DT.Ticks;
            if (span > 10000000)
            {
                this.Text = "fps : " + ticks  + " time" + time + "ns";
                DT = DateTime.Now;
                ticks = 0;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            DT = DateTime.Now;

            T.Start();
        }

        double time = 0;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            time = DateTime.Now.Ticks;
            e.Graphics.DrawImage(Bmps[R.Next(BmpCount)], 0, 0);
            time = (DateTime.Now.Ticks - time) / 10000;

        }
    }
}
